package me.tuyou.activiti.taskService;

import me.tuyou.activiti.Base;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.junit.Test;

import java.util.List;

public class CandidateUsers extends Base {

    @Test
    public void deploy() {
        repositoryService.createDeployment()
                .addClasspathResource("CandidateUsers.bpmn")
                .name("候选用户")
                .deploy();
    }

    @Test
    public void startInstance() {
        runtimeService.startProcessInstanceByKey("CandidateUsers");
    }

    /**
     * 用这种方式搜不出来
     */
    @Test
    public void queryAssignmentTask() {
        List<Task> list = taskService.createTaskQuery().taskAssignee("tuyou").list();
        for (Task tuyou : list) {
            System.out.println(ReflectionToStringBuilder.toString(tuyou));
        }
    }

    /**
     * 这种可以搜出来
     */
    @Test
    public void queryCandidateTask() {

        List<Task> tuyou = taskService.createTaskQuery().taskCandidateUser("tuyou").list();
        System.out.println("tuyou list------------------------------");
        for (Task task : tuyou) {
            System.out.println(task.getId() + " " + task.getName());

        }


        List<Task> yangrui = taskService.createTaskQuery().taskCandidateUser("yangrui").list();
        System.out.println("yangrui list--------------------------------");
        for (Task task : yangrui) {
            System.out.println(task.getId() + " " + task.getName());
        }


        for (Task task : tuyou) {
            taskService.complete(task.getId());
            checkProcessStop(task.getProcessInstanceId());
        }

        for (Task task : yangrui) {
            taskService.complete(task.getId());
            checkProcessStop(task.getProcessInstanceId());
        }
    }

    /**
     * 查询这个任务有哪些候选人
     */
    @Test
    public void queryCadidateUsers() {
        List<IdentityLink> identityLinksList = taskService.getIdentityLinksForTask("162504");
        for (IdentityLink link : identityLinksList) {
            System.out.println(link.getUserId());
        }

    }
}
